const {createOrder, findAllOrder,updateOrder}=require('../service/order.service')
class OrderController {
    //创建订单
    async create(ctx){
        //解析参数
        const user_id=ctx.state.user.id;
        const {address_id,goods_info,total}=ctx.request.body;
        //订单编号
        const order_number='XZD'+Date.now()

        //数据库
        try{
            const res=await createOrder({
                order_number,
                user_id,
                address_id,
                goods_info,
                total,
                
            })
            ctx.body={
                code:200,
                message:'创建订单成功',
                result:res 
            }

        } catch(err){
            console.error(err);
        }
       
    }
    //获取订单列表
    async findAll(ctx){
      const{pageNum=1,pageSize=10,status=0}=ctx.request.query
      try{
        const res=await findAllOrder(pageNum,pageSize,status)
        ctx.body={
          code:200,
          message:'获取订单列表成功',
          data:res 
        } 
      } catch(err){
        console.error(err); 
      }
    
    
    }
    //更新订单状态
    async update(ctx){
        const id=ctx.params.id;
        const {status}=ctx.request.body
        try{
            const res=await updateOrder(id,status) 
            ctx.body={
                code:200,
                message:'更新订单状态成功',
                data:res 
            }
        } catch(err){
            console.error(err);
        }
    }

}



module.exports = new OrderController()